using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnvManagers : MonoBehaviour
{

    [SerializeField]
    private Camera MainCamera;

    /// <summary>
    /// �߽�ֵ
    /// </summary>
    public float minX;
    public float maxX;
    public float minZ;
    public float maxZ;

    Texture2D bottomPannel;

    private Vector3 leftBottom;
    private Vector3 rightTop;

    private List<Transform> transforms = new List<Transform>();

    private float _sliderValue = 1f;

    public float SliderValue
    {
        get
        {
            return _sliderValue;
        }
        set
        {
            Debug.Log("sliderValue is " + value);
            _sliderValue = value;
        }
    }

    void Awake()
    {
        GameManager.Instance().envManagers = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Start");

    }

    public void Init()
    {
        _InitChilds();
        AdjustCamera();
        _InitEadge();
    }

    private void _InitChilds()
    {
        int childCount = transform.childCount;
        Debug.Log("childCount is " + childCount);
        for (int i = 0; i < childCount; i++)
        {
            Transform item = transform.GetChild(i);
            transforms.Add(item);
        }
    }

    void AdjustCamera()
    {
        if (MainCamera.orthographic)
        {
            float targetAspect = (float)750 / (float)1334;
            float size = MainCamera.orthographicSize;

            float screenAspect = (float)Screen.width / (float)Screen.height;
            Debug.Log("screenAspect is " + screenAspect);
            Debug.Log("targetAspect is " + targetAspect);

            float ratio = (screenAspect / targetAspect);
            Debug.Log("ratio is " + ratio);

            float targetSize = (size * screenAspect) / targetAspect;

            Debug.Log("targetSize is " + targetSize);
            MainCamera.orthographicSize = targetSize;

            GameManager.Instance().topUI.AdapterUICamera();
        }
        else
        {
            float fov = MainCamera.fieldOfView;
            float originRatio = (float)750 / 1334;
            float curRatio = (float)Screen.width / Screen.height;

            float ratio = curRatio / originRatio;
            MainCamera.fieldOfView = fov * ratio;

            Debug.Log("cur fov is " + MainCamera.fieldOfView);
        }
    }

    private void _InitEadge()
    {

        leftBottom = MainCamera.ViewportToWorldPoint(new Vector3(0, 0, MainCamera.transform.position.y));
        rightTop = MainCamera.ViewportToWorldPoint(new Vector3(1, 1, MainCamera.transform.position.y));

        MainCamera.orthographic = true;
        Debug.Log("leftBottom is " + leftBottom);
        Debug.Log("rightTop is " + rightTop);


        Transform leftTrans = (transforms[0] as Transform);
        Transform rightTrans = (transforms[0] as Transform);
        Transform topTrans = (transforms[0] as Transform);
        Transform bottomTrans = (transforms[0] as Transform);

        minX = leftBottom.x;
        maxX = rightTop.x;
        minZ = leftBottom.z + 250f;
        maxZ = rightTop.z - 150f;

        (transforms[0] as Transform).position = new Vector3(minX, leftTrans.position.y, leftTrans.position.z);
        (transforms[1] as Transform).position = new Vector3(maxX, rightTrans.position.y, rightTrans.position.z);
        (transforms[2] as Transform).position = new Vector3(topTrans.position.x, topTrans.position.y, maxZ);
        (transforms[3] as Transform).position = new Vector3(bottomTrans.position.x, bottomTrans.position.y, minZ);

        Debug.Log($"minZ is {minZ} maxZ is {maxZ}");

    }

    private void OnGUI()
    {
        // float value = GUI.HorizontalSlider(new Rect(200, 400, 400f, 100f), SliderValue, 1f, 10f);
        // if (value != SliderValue)
        // {
        //     SliderValue = value;
        // }
    }

    private void OnDrawGizmos()
    {
        /*Gizmos.color = Color.yellow;
        Gizmos.DrawSphere(leftBottom, 0.1f);

        Gizmos.DrawSphere(rightTop, 0.1f);
        if(minX != 0 && maxX != 0 && minZ != 0 && maxZ != 0)
        {
            Debug.Log("draw debug");
            // left
            Gizmos.DrawSphere(new Vector3(minX,2,0),0.5f);
            Gizmos.DrawSphere(new Vector3(maxX, 2, 0), 0.5f);
            Gizmos.DrawSphere(new Vector3(0, 2, minZ + 4), 0.5f);
            Gizmos.DrawSphere(new Vector3(0, 2, maxZ), 0.5f);
        }*/
    }



    // Update is called once per frame
    void Update()
    {

    }
}
